import threading
import queue
import time
import tkinter as tk
from tkinter import ttk, messagebox
from serial import Serial, SerialException
# 数据管理类
class NodeDataManager:
    def __init__(self, num_nodes=5):
        self.num_nodes = num_nodes
        self.lock = threading.Lock()  # 线程锁
        self.serial_connected = False
        self.serial_port = None

        # 初始化默认数据
        self.nodes = {
            i: {
                "position": [0.0, 0.0, 0.0],
                "status": "离线",
                "neighbors rssi":[0 for i in range(self.num_nodes)],
                "timestamp": time.time(),
                "energy":0,
            } for i in range(self.num_nodes)
        }

    def connect_serial(self, port, baudrate):
        """连接串口"""
        try:
            self.serial_port = Serial(port, baudrate, timeout=1)
            self.serial_connected = True
            return True
        except SerialException as e:
            print(f"串口连接失败: {str(e)}")
            self.serial_connected = False
            return False

    def disconnect_serial(self):
        """断开串口连接"""
        if self.serial_connected:
            self.serial_port.close()
            self.serial_connected = False

    def parse_input(self,raw_data):#未完成
        """此处解析报文数据"""
        node_id = 0
        payload = []
        return node_id,payload

    def update_data(self, raw_data): #参数输入为串口数据
        """更新节点数据"""
        node_id,payload = self.parse_input(raw_data)#解析报文
        """
        with self.lock:
            self.nodes[id]["status"] = "在线“
        """#类似于这种方式更新数据



    def get_node_data(self, node_id):
        """安全获取节点数据"""
        with self.lock:
            return self.nodes.get(node_id, {}).copy()  # 返回数据副本

    def get_all_data(self):
        """获取所有节点数据快照"""
        with self.lock:
            return {k: v.copy() for k, v in self.nodes.items()}

if __name__ == "main":
    data_manager = NodeDataManager()